﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.TRDSelector;
using MS.InformationManagement.DAL.RecordCenter;
using System.Collections.Generic;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;
using System.Linq;

namespace MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls
{
    public partial class TRDSelector : UserControl, ITRDSelectorVIew
    {

        #region fields

        TRDSelectorPresenter _presenter;
        int? _selectedSubSection;
        int? _selectedSubSerie;
        int _selectedSection;
        int _selectedSerie;
        int? _selectedTipoDocumentalID;

        #endregion

        #region Properties

        public bool BelongsToCurrentTRD
        {
            get
            {
                return ViewState["BelongsToCurrentTRD"] == null ? true : Convert.ToBoolean(ViewState["BelongsToCurrentTRD"]);
            }
            set { ViewState["BelongsToCurrentTRD"] = value; }
        }

        public string SelectedTipoDocumentalName
        {
            get { return ddlDocumentType.SelectedValue.Equals("0") ? string.Empty : ddlDocumentType.SelectedItem.Text; }
        }

        public IEnumerable<Seccion> Secciones { get; set; }

        public IEnumerable<Seccion> SubSecciones { get; set; }

        public IEnumerable<Serie> Serie { get; set; }

        public IEnumerable<Subserie> SubSerie { get; set; }

        public IEnumerable<TipoDocumental> TiposDocumentales { get; set; }

        public int? SelectedTipoDocumentalID
        {
            get
            {
                if (_selectedTipoDocumentalID.HasValue && _selectedTipoDocumentalID != 0)
                    return _selectedTipoDocumentalID;
                else if (ddlDocumentType.SelectedIndex == 0 || ddlDocumentType.SelectedIndex == -1)
                    return 0;
                else
                    return string.IsNullOrEmpty(ddlDocumentType.SelectedValue) ? 0 : int.Parse(ddlDocumentType.SelectedValue);
            }
            set
            {
                _selectedTipoDocumentalID = value;
            }
        }

        public int SelectedSection
        {
            get
            {
                if (_selectedSection != 0)
                    return _selectedSection;
                else
                    return string.IsNullOrEmpty(ddlSeccion.SelectedValue) ? 0 : int.Parse(ddlSeccion.SelectedValue);
            }
            set
            {
                _selectedSection = value;
            }
        }

        public int SelectedSerie
        {
            get
            {
                if (_selectedSerie != 0)
                    return _selectedSerie;
                else
                    return string.IsNullOrEmpty(ddlSerie.SelectedValue) ? 0 : int.Parse(ddlSerie.SelectedValue);
            }
            set
            {
                _selectedSerie = value;
            }
        }

        public int? SelectedSubSection
        {
            get
            {
                if (_selectedSubSection.HasValue && _selectedSubSection != 0)
                    return _selectedSubSection;
                else if (ddlSubSeccion.SelectedIndex == 0 || ddlSubSeccion.SelectedIndex == -1)
                    return 0;
                else
                    return string.IsNullOrEmpty(ddlSubSeccion.SelectedValue) ? 0 : int.Parse(ddlSubSeccion.SelectedValue);
            }
            set
            {
                _selectedSubSection = value;
            }
        }

        public int? SelectedSubSerie
        {
            get
            {
                if (_selectedSubSerie.HasValue && _selectedSubSerie != 0)
                    return _selectedSubSerie;
                else if (ddlSubSerie.SelectedIndex == 0 || ddlSubSerie.SelectedIndex == -1)
                    return 0;
                else
                    return string.IsNullOrEmpty(ddlSubSerie.SelectedValue) ? 0 : int.Parse(ddlSubSerie.SelectedValue);
            }
            set
            {
                _selectedSubSerie = value;
            }
        }

        public bool Enable
        {
            set
            {
                ddlSeccion.Enabled = value;
                ddlSubSeccion.Enabled = value;
                ddlSubSerie.Enabled = value;
                ddlSerie.Enabled = value;
                ddlDocumentType.Enabled = value;
            }
        }

        public bool DisableTRDExceptDocumentalType
        {
            set
            {
                ddlSeccion.Enabled = !value;
                ddlSubSeccion.Enabled = !value;
                ddlSubSerie.Enabled = !value;
                ddlSerie.Enabled = !value;
            }
        }

        public bool Required { get; set; }

        public bool ReqTRD()
        {
            if (SelectedSection > 0 || SelectedSubSection > 0 || SelectedSerie > 0 || SelectedSubSerie > 0 || SelectedTipoDocumentalID > 0)
                if (SelectedSubSection <= 0 || SelectedSubSerie <= 0)
                    return true;
                else
                    return false;
            else
                return false;
        }

        ListItem item = new ListItem(GlobalConstants.DDL_INITIAL_DOCUMENT_TYPE, "0");

        #endregion

        #region Form Events

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new TRDSelectorPresenter(this);
            if (IsPostBack) return;
            _presenter.HandleLoad();
            if (OnPageLoadComplete != default(PageLoadHandler)) { OnPageLoadComplete(this, new EventArgs()); }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (Required)
                SetRequired();
        }

        protected void Seccion_Change(object sender, EventArgs e)
        {
            _presenter.HandleLoadSubSeccion();
            ClearTipoDocumental();
            LoadTipoDocumental();
        }

        protected void SubSeccion_Change(object sender, EventArgs e)
        {
            ClearTipoDocumental();
            LoadTipoDocumental();
        }

        protected void Series_Change(object sender, EventArgs e)
        {
            _presenter.HandleLoadSubSeries();
            ClearTipoDocumental();
            LoadTipoDocumental();
        }

        protected void SubSeries_Change(object sender, EventArgs e)
        {
            _presenter.LoadTiposDocumentales();
            LoadTipoDocumental();
        }

        #endregion

        #region Implemented Methods

        public void SetUserDependency(string userSectionName)
        {
            if (_presenter == null) { _presenter = new TRDSelectorPresenter(this); }
            _presenter.SetUserDependency(userSectionName);
        }

        public void SetCurrentUserDependency()
        {
            if (_presenter == null) { _presenter = new TRDSelectorPresenter(this); }
            _presenter.SetCurrentUserDependency();
        }

        public void Show()
        {
            ddlSeccion.DataSource = Secciones;
            ddlSeccion.DataBind();
            ddlSeccion.Items.Insert(0, new ListItem() { Value = "0", Text = GlobalConstants.DDL_INITIAL_SECTION });
            ddlSerie.DataSource = Serie;
            ddlSerie.DataBind();
            ddlSerie.Items.Insert(0, new ListItem() { Value = "0", Text = GlobalConstants.DDL_INITIAL_SERIE });
            ddlSubSerie.DataSource = SubSerie;
            ddlSubSerie.DataBind();
            ddlSubSerie.Items.Insert(0, new ListItem() { Value = "0", Text = GlobalConstants.DDL_INITIAL_SUBSERIE });
            ddlSubSeccion.DataSource = SubSecciones;
            ddlSubSeccion.DataBind();
            ddlSubSeccion.Items.Insert(0, new ListItem() { Value = "0", Text = GlobalConstants.DDL_INITIAL_SUBSECTION });
            ddlDocumentType.DataSource = TiposDocumentales;
            ddlDocumentType.DataBind();
            if (!ddlDocumentType.Items.Contains(item)) { ddlDocumentType.Items.Insert(0, item); }
        }

        public void LoadSubSeccion()
        {
            ddlSubSeccion.DataSource = SubSecciones;
            ddlSubSeccion.DataBind();
            ddlSubSeccion.Items.Insert(0, new ListItem() { Value = "0", Text = GlobalConstants.DDL_INITIAL_SUBSECTION });

        }

        public void LoadSubSerie()
        {
            ddlSubSerie.DataSource = SubSerie;
            ddlSubSerie.DataBind();
            ddlSubSerie.Items.Insert(0, new ListItem() { Value = "0", Text = GlobalConstants.DDL_INITIAL_SUBSERIE });
        }

        public void LoadSelectValues()
        {

            ddlSerie.SelectedIndex = ddlSerie.Items.IndexOf((from item in ddlSerie.Items.OfType<ListItem>()
                                                             where item.Value == SelectedSerie.ToString()
                                                             select item).FirstOrDefault());
            ddlSeccion.SelectedIndex = ddlSeccion.Items.IndexOf((from item in ddlSeccion.Items.OfType<ListItem>()
                                                                 where item.Value == SelectedSection.ToString()
                                                                 select item).FirstOrDefault());
            ddlSubSerie.SelectedIndex = ddlSubSerie.Items.IndexOf((from item in ddlSubSerie.Items.OfType<ListItem>()
                                                                   where item.Value == SelectedSubSerie.Value.ToString()
                                                                   select item).FirstOrDefault());
            ddlSubSeccion.SelectedIndex = ddlSubSeccion.Items.IndexOf((from item in ddlSubSeccion.Items.OfType<ListItem>()
                                                                       where item.Value == SelectedSubSection.Value.ToString()
                                                                       select item).FirstOrDefault());
            ddlDocumentType.SelectedIndex = ddlDocumentType.Items.IndexOf((from item in ddlDocumentType.Items.OfType<ListItem>()
                                                                           where item.Value == SelectedTipoDocumentalID.Value.ToString()
                                                                           select item).FirstOrDefault());
        }

        public void LoadTipoDocumental()
        {
            ddlDocumentType.DataSource = TiposDocumentales;
            ddlDocumentType.DataBind();
            if (!ddlDocumentType.Items.Contains(item)) { ddlDocumentType.Items.Insert(0, item); }
            ddlDocumentType.SelectedIndex = 0;
        }

        private void ClearTipoDocumental()
        {
            ddlDocumentType.Items.Clear();
            if (!ddlDocumentType.Items.Contains(item)) { ddlDocumentType.Items.Insert(0, item); }
            ddlDocumentType.SelectedIndex = 0;
            _presenter.LoadTiposDocumentales();
        }

        #endregion

        #region Public Methods

        public void SetInitialValues()
        {
            //_presenter = new TRDSelectorPresenter(this);
            //_presenter.LoadInitialValues();
        }

        public void SetRequired()
        {
            Label1.Text = "Sección *";
            Label2.Text = "Subsección *";
            Label3.Text = "Serie *";
            Label4.Text = "Subserie *";
            Label5.Text = "Tipo documento *";

            ddlSubSeccion.Required("Debe seleccionar una subseccion", "ValBtnAddRadicado", AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
            ddlSubSerie.Required("Debe seleccionar una subserie", "ValBtnAddRadicado", AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
            ddlDocumentType.Required("Debe seleccionar un tipo documental", "ValBtnAddRadicado", AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
        }

        public void CheckTRDStatus()
        {
            _presenter = new TRDSelectorPresenter(this);
            _presenter.CheckCurrentTRD();
            Enable = this.BelongsToCurrentTRD;
        }


        public event PageLoadHandler OnPageLoadComplete;
        public delegate void PageLoadHandler(object sender, EventArgs e);

        #endregion

    }
}
